package com.inspur.his.common;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    /**
     * 对明文字符串进行md5加密
     * @param source 传入的字符串
     * @return 加密的结果
     */
    public static String md5(String source) {
        String en_pass = "";//加密后的密码
        // 1.判断source是否有效
        if (source != null && source.length() > 0) {
            // 2.如果不是有效的字符串抛出异常
            try {
                // 3.获取MessageDigest对象
                String algorithm = "md5";
                MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
                // 4.获取明文字符串对应的字节数组
                byte[] input = source.getBytes();
                // 5.执行加密
                byte[] output = messageDigest.digest(input);
                // 6.创建BigInteger对象
                int signum = 1;
                BigInteger bigInteger = new BigInteger(signum, output);
                // 7.按照16进制将bigInteger的值转换为字符串
                int radix = 16;
                 en_pass = bigInteger.toString(radix).toUpperCase();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return en_pass;
    }

    public static void main(String[] args) {
        String password="666666";
        String en_password = md5(password);
        System.out.println("en_pass:"+en_password);
        //123456:E10ADC3949BA59ABBE56E057F20F883E   DES:48b5b6bcc6b33659
        //666666:F379EAF3C831B04DE153469D1BEC345E   DES:0df8120c182ec1b3

    }
}
